import { Destroyable } from "xbsj-base";
import { ESViewer } from "./index";
import { ESVOption, ViewerObjsMap } from "../ESJTypes";
export class ViewerContext extends Destroyable {
    // registerViewers: { [key: string]: ViewerObjsMap } = {};
    private _registerObjsMap: ViewerObjsMap = new Map();
    get registerObjsMap() { return this._registerObjsMap };
    set registerObjsMap(value: ViewerObjsMap) { this._registerObjsMap = value; }

    register<T extends ESViewer>(viewerType: string, viewerConstructor: new (option: ESVOption) => T) {
        if (this._registerObjsMap.has(viewerType)) {
            console.warn(`register warn: ${viewerType} has registered !`);
        }
        this._registerObjsMap.set(viewerType, viewerConstructor);
        return viewerType;
    }

    getViewerConstructor<T extends ESViewer>(viewerType: string) {
        //@ts-ignore
        const map = window.ESSDK_ViewerObjs[viewerType] ?? new Map();
        const viewerConstructor = map.get(viewerType);
        if (!viewerConstructor) {
            console.warn(`未找到${viewerType}类,如果该对象有引擎实现类,必须在new ESObjectsManager(ESCesiumViewer,...)时传入引擎视口类参数,否则可能无法整合该引擎的注册对象！`)
            return undefined;
        }
        return viewerConstructor as unknown as (new (option: ESVOption) => T);
    }

    createViewer<T extends ESViewer>(option: ESVOption) {
        const viewerType = option.type;
        const viewerConstructor = this.getViewerConstructor(viewerType);
        if (!viewerConstructor) {
            throw new Error(`没有找到类型为${viewerType}的Viewer构造器!`);
        }
        const viewer = new viewerConstructor(option) as T;
        return viewer;
    }
}
